package com.cash.app.application

import android.app.Application
import android.util.Log
import com.cash.app.BuildConfig
import com.cash.app.constant.AppConstant
import com.cash.app.manager.GlobalTaskManager
import com.cash.app.net.RetrofitClient
import com.cash.app.utils.GPAIdUtils
import com.cash.app.utils.GPAdjust
import com.cash.app.utils.GPReferrerUtils
import com.cash.app.utils.SpUtils
import com.cash.app.utils.TLog
import com.cash.app.utils.extra.ExtraInfoUtils

/**
 * Application initialization manager
 * Manages both basic initialization and network-authorized initialization
 */
object AppInitializer {
    private const val TAG = "AppInitializer"

    fun startInit(application: Application) {
        TLog.d("AppInitializer started", TAG)
        initBasic(application)
        if (SpUtils.get(AppConstant.K_PP_GRANTED, false)) {
            initAfterGranted(application)
        }
    }

    /**
     * Basic initialization, do not need granted.
     */
    private fun initBasic(application: Application) {
        // Initialize log
        TLog.initTLog(isShow = true, if (BuildConfig.DEBUG) Log.DEBUG else Log.ERROR)
        // Initialize SharedPreferences
        SpUtils.init(application)
    }

    /**
     * called after Policy&Permissions granted.
     */
    private fun initAfterGranted(application: Application) {
        // Initialize network service
        RetrofitClient.init()

        // extra device info
        ExtraInfoUtils.init()

        // Initialize Google Advertising ID
        GPAIdUtils.init(application)

        // Initialize Google Play Referrer
        GPReferrerUtils.init(application)

        // Initialize Adjust SDK
        GPAdjust.init(application)

        // GlobalTaskManager start all tasks
        GlobalTaskManager.startAllTask()

        TLog.d("AppInitializer completed", TAG)
    }
} 